package com.qin.mall.controller.productcontroller;

import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.domain.PmsProductCategory;
import com.qin.mall.dto.PmsProductCategoryWithChildrenItem;
import com.qin.mall.dto.ProductCategoryParam;
import com.qin.mall.openfeign.product.ProductCategoryFeigin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品分类管理
 * @author Leisure
 * create 2022 2022/11/19 17:12
 **/
@RestController
@RequestMapping("/productCategory")
public class CategoryController {
    
    @Autowired
    private ProductCategoryFeigin categoryFeigin;

    //创建商品分类
    @PostMapping("/create")
    public CommonResult createCategory(@RequestBody ProductCategoryParam productCategoryParam){
        CommonResult category = categoryFeigin.createCategory(productCategoryParam);
        return category;
    }

    //根据id修改商品 分类
    @PostMapping("/update/{id}")
    public CommonResult updateCategory(@PathVariable Long id,
                                       @RequestBody ProductCategoryParam productCategoryParam){
        CommonResult count = categoryFeigin.updateCategory(id, productCategoryParam);
        return count;
    }

    //分页获取商品分类
    @GetMapping("/list/{parentId}")
    @ResponseBody
    public CommonResult<MyBatisPlusPage<PmsProductCategory>> getList(@PathVariable Long parentId,
                                                                @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                                                @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ){
        CommonResult<MyBatisPlusPage<PmsProductCategory>> list = categoryFeigin.getList(parentId, pageSize, pageNum);
        return list;
    }

    //根据ID获取商品分类
    @GetMapping("/{id}")
    public CommonResult<PmsProductCategory> selectById(@PathVariable Long id){
        CommonResult<PmsProductCategory> result = categoryFeigin.selectById(id);
        return result;
    }

    // 根据id删除商品分类
    @PostMapping("/delete/{id}")
    public CommonResult deleteById(@PathVariable Long id){
        CommonResult count = categoryFeigin.deleteById(id);
        return count;
    }

    // 批量修改显示状态
    @PostMapping("/update/showStatus")
    public CommonResult updateShowStatus(@RequestParam("showStatus") Integer showStatus,
                                         @RequestParam("ids") List<Long> ids){
        CommonResult count = categoryFeigin.updateShowStatus(showStatus, ids);
        return count;
    }

    // 批量修改导航状态
    @PostMapping("/update/navStatus")
    public CommonResult updateNavStatus(@RequestParam("navStatus") Integer navStatus,
                                        @RequestParam("ids") List<Long> ids){
        CommonResult count = categoryFeigin.updateNavStatus(navStatus, ids);
        return count;
    }

    //以层级形式获取商品分类
    @GetMapping("/list/withChildren")
    public CommonResult<List<PmsProductCategoryWithChildrenItem>> withChildren(){
        CommonResult<List<PmsProductCategoryWithChildrenItem>> result = categoryFeigin.withChildren();
        return result;
    }
    
}
